package cn.ekuma.epos.analysis.front.customerAction;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.tree.TreeNode;

import org.jdesktop.swingx.treetable.AbstractTreeTableModel;
import org.jdesktop.swingx.treetable.TreeTableModel;
import org.jdesktop.swingx.treetable.TreeTableNode;

import com.openbravo.bean.erp.viewbean.ProductCategoryAnalysisInfo;
import com.openbravo.bean.erp.viewbean.ProductCategoryTransaction;
import com.openbravo.data.basic.BasicException;
import com.openbravo.format.Formats;
import com.openbravo.pos.base.AppLocal;

import cn.ekuma.data.dao.bean.I_Category;
import cn.ekuma.epos.analysis.front.customerAction.model.ProductCategoryTreeTableModel;
import cn.ekuma.epos.datalogic.I_DataLogicERP;

public class AnalysisModelManager {

	 private ProductCategoryTreeTableNode rootNode;
	 private ProductCategoryTreeTableModel treeTableModel;
	 List<ProductCategoryAnalysisInfo> productCategoryAnalysisInfoList;
	 I_DataLogicERP dlSales;
	
	 
	public AnalysisModelManager(I_DataLogicERP dlSales) {
		this.dlSales = dlSales;
	    rootNode= new ProductCategoryTreeTableNode(null);
	    treeTableModel=new ProductCategoryTreeTableModel(rootNode);
	}

	public TreeTableModel getTreeTableModel(){
       return treeTableModel;
   }

	public void query(Object filter) throws BasicException{
		rootNode= new ProductCategoryTreeTableNode(null);
		productCategoryAnalysisInfoList=dlSales.listPosCategoryAnalysisInfo(filter);
	    buildTreeForList(productCategoryAnalysisInfoList,rootNode);  
		treeTableModel.reflush(rootNode);
	}
	
	
	private void buildTreeForList(List<ProductCategoryAnalysisInfo>  m_aData,ProductCategoryTreeTableNode node){
		if(m_aData.isEmpty())
			return;
		List<ProductCategoryAnalysisInfo> recapt=new ArrayList();
		ProductCategoryTreeTableNode temp;
		for(ProductCategoryAnalysisInfo c:m_aData){
			if(node.isChinad(c)){
				temp=new ProductCategoryTreeTableNode(c);
				temp.setParent(node);
				node.insert(temp,node.getChildCount());
			}else 
				recapt.add(c);
		}
		for(int i=0;i<node.getChildCount();i++)
			buildTreeForList(recapt,(ProductCategoryTreeTableNode) node.getChildAt(i));
	}
	
	
	public static String[] COLUMNNAME_ProductCategoryAnalysisInfo={
		 AppLocal.getIntString("label.prodcategory"),
		 AppLocal.getIntString("label.units"),
		 AppLocal.getIntString("label.prodpricebuy"),
	     AppLocal.getIntString("label.prodpricesell"),
	     AppLocal.getIntString("label.profits")
	 };
	
	class ProductCategoryTreeTableNode implements TreeTableNode {
		ProductCategoryTreeTableNode parent;
		ProductCategoryAnalysisInfo node;
		
		Map<String,ProductCategoryTransaction> transaction=new HashMap<String,ProductCategoryTransaction>();
		List<ProductCategoryTreeTableNode> childs=new ArrayList();
		
		
		public ProductCategoryTreeTableNode(
				ProductCategoryTreeTableNode parent,
				ProductCategoryAnalysisInfo node) {
			this.parent = parent;
			this.node = node;
		}

		public void insert(ProductCategoryTreeTableNode temp, int childCount) {
			childs.add(childCount, temp);
		}

		public ProductCategoryTreeTableNode(ProductCategoryAnalysisInfo node ) {
			this.node = node;
		}

		public boolean isChinad(I_Category category1) {
			boolean isChinad=false;
			if(node==null){
				if(category1.getM_sParentID()==null)
					isChinad=true;
			}else if(node.getID().equalsIgnoreCase(category1.getM_sParentID()))
				isChinad=true;
			return  isChinad;
		}

		@Override
		public int getChildCount() {	
			return childs.size();
		}

		@Override
		public int getIndex(TreeNode node) {
			return 0;
		}

		@Override
		public boolean getAllowsChildren() {
			return !childs.isEmpty();
		}

		@Override
		public boolean isLeaf() {
			return childs.isEmpty();
		}

		@Override
		public Enumeration<? extends TreeTableNode> children() {
			throw new UnsupportedOperationException("Not supported yet.");
		}

		@Override
		public TreeTableNode getChildAt(int i) {
			return childs.get(i);
		}

		@Override
		public int getColumnCount() {
			return COLUMNNAME_ProductCategoryAnalysisInfo.length;
		}

		@Override
		public TreeTableNode getParent() {
			return this.parent;
		}

		@Override
		public Object getUserObject() {	
			return node;
		}

		@Override
		public Object getValueAt(int column) {
			if(node==null)
				return null;
			
				switch(column){
			       	case  0:
			       		return node.getName();
			       	case 1:
			       		return node.getMultiply();
			       	case 2:
			       		return Formats.CURRENCY.formatValue(node.getBuyPrice());
			       	case 3:
			       		return Formats.CURRENCY.formatValue(node.getSellPrice());
			       	case 4:
			       		return Formats.PERCENT.formatValue(node.getProfits());
			}
			return null;
		}

		private ProductCategoryTransaction getTransaxtion(String string) {
			return transaction.get(string);
		}

		@Override
		public boolean isEditable(int arg0) {
			return false;
		}

		@Override
		public void setUserObject(Object arg0) {
		}

		@Override
		public void setValueAt(Object arg0, int arg1) {
		}

		public ProductCategoryAnalysisInfo getNode() {
			return node;
		}

		public void setNode(ProductCategoryAnalysisInfo node) {
			this.node = node;
		}

		public Map<String,ProductCategoryTransaction> getIncludeTransaction() {
			return transaction;
		}

		public void setIncludeTransaction(
				Map<String,ProductCategoryTransaction> includeTransaction) {
			transaction = includeTransaction;
		}

		public List<ProductCategoryTreeTableNode> getChilds() {
			return childs;
		}

		public void setChilds(List<ProductCategoryTreeTableNode> childs) {
			this.childs = childs;
		}

		public void setParent(ProductCategoryTreeTableNode parent) {
			this.parent = parent;
		}
		
		
	}
}
